<?php
namespace app\service;

/**
 * 设备指令模板服务
 */
class DeviceCommandTemplates
{
    /**
     * 获取所有支持的指令类型
     * 
     * @return array
     */
    public static function getSupportedTypes(): array
    {
        return [
            'reboot' => [
                'name' => '设备重启',
                'description' => '立即重启设备',
                'params' => []
            ],
            'power_off' => [
                'name' => '设备关机',
                'description' => '关闭设备电源',
                'params' => []
            ],
            'factory_reset' => [
                'name' => '恢复出厂设置',
                'description' => '将设备恢复到出厂状态',
                'params' => []
            ],
            'hotspot_enable' => [
                'name' => '开启热点',
                'description' => '启用WiFi热点功能',
                'params' => []
            ],
            'hotspot_disable' => [
                'name' => '关闭热点',
                'description' => '禁用WiFi热点功能',
                'params' => []
            ],
            'hotspot_config' => [
                'name' => '配置热点',
                'description' => '设置热点名称和密码为默认值',
                'params' => []
            ],
            'sim_switch_slot1' => [
                'name' => '切换到SIM卡槽1',
                'description' => '切换到第一个SIM卡槽',
                'params' => []
            ],
            'sim_switch_slot2' => [
                'name' => '切换到SIM卡槽2',
                'description' => '切换到第二个SIM卡槽',
                'params' => []
            ],
            'speed_limit_10m' => [
                'name' => '限速10MB',
                'description' => '设置网络限速为10MB/s',
                'params' => []
            ],
            'speed_limit_50m' => [
                'name' => '限速50MB',
                'description' => '设置网络限速为50MB/s',
                'params' => []
            ],
            'speed_limit_100m' => [
                'name' => '限速100MB',
                'description' => '设置网络限速为100MB/s',
                'params' => []
            ],
            'speed_unlimited' => [
                'name' => '取消限速',
                'description' => '取消网络限速限制',
                'params' => []
            ],
            'report_status' => [
                'name' => '立即上报状态',
                'description' => '设备立即上报当前状态信息',
                'params' => []
            ],
            'device_enable' => [
                'name' => '启用设备',
                'description' => '启用设备网络功能',
                'params' => []
            ],
            'device_disable' => [
                'name' => '禁用设备',
                'description' => '禁用设备网络功能',
                'params' => []
            ],
            'hotspot_modify' => [
                'name' => '修改热点配置',
                'description' => '自定义修改WiFi热点配置信息',
                'params' => [
                    'hotspot_name' => '热点名称',
                    'hotspot_password' => '热点密码',
                    'hotspot_connected_num_max' => '最大连接数',
                    'hotspot_get_hidden' => '是否隐藏(0=不隐藏,1=隐藏)',
                    'hotspot_band' => '频段(1=2.4G,2=5G)'
                ]
            ]
        ];
    }
    
    /**
     * 根据类型获取MQTT指令
     *
     * @param string $type 指令类型
     * @param array $params 指令参数
     * @return array|null MQTT指令数组
     */
    public static function getCommandByType(string $type, array $params = []): ?array
    {
        $commands = [
            'reboot' => [
                'cmds' => [
                    ['cmd_name' => 'reboot']
                ]
            ],
            'power_off' => [
                'cmds' => [
                    ['cmd_name' => 'power_off']
                ]
            ],
            'factory_reset' => [
                'cmds' => [
                    ['cmd_name' => 'recovery']
                ]
            ],
            'hotspot_enable' => [
                'cmds' => [
                    ['cmd_name' => 'hotspot_on']
                ]
            ],
            'hotspot_disable' => [
                'cmds' => [
                    ['cmd_name' => 'hotspot_off']
                ]
            ],
            'hotspot_config' => [
                'cmds' => [
                    [
                        'cmd_name' => 'hotspot_modify',
                        'cmd_params' => [
                            'hotspot_name' => 'Device_WiFi',
                            'hotspot_password' => '12345678',
                            'hotspot_connected_num_max' => 10,
                            'hotspot_whether_hide' => 0,
                            'hotspot_band' => 1
                        ]
                    ]
                ]
            ],
            'hotspot_modify' => [
                'cmds' => [
                    [
                        'cmd_name' => 'hotspot_modify',
                        'cmd_params' => [] // 将由传入的参数填充
                    ]
                ]
            ],
            'sim_switch_slot1' => [
                'cmds' => [
                    [
                        'cmd_name' => 'sim_switch',
                        'cmd_params' => [
                            'sim_id' => 0
                        ]
                    ]
                ]
            ],
            'sim_switch_slot2' => [
                'cmds' => [
                    [
                        'cmd_name' => 'sim_switch',
                        'cmd_params' => [
                            'sim_id' => 2
                        ]
                    ]
                ]
            ],
            'speed_limit_10m' => [
                'cmds' => [
                    [
                        'cmd_name' => 'network_speed_limit',
                        'cmd_params' => [
                            'type' => 'all',
                            'unit' => 'MB',
                            'value' => 10
                        ]
                    ]
                ]
            ],
            'speed_limit_50m' => [
                'cmds' => [
                    [
                        'cmd_name' => 'network_speed_limit',
                        'cmd_params' => [
                            'type' => 'all',
                            'unit' => 'MB',
                            'value' => 50
                        ]
                    ]
                ]
            ],
            'speed_limit_100m' => [
                'cmds' => [
                    [
                        'cmd_name' => 'network_speed_limit',
                        'cmd_params' => [
                            'type' => 'all',
                            'unit' => 'MB',
                            'value' => 100
                        ]
                    ]
                ]
            ],
            'speed_unlimited' => [
                'cmds' => [
                    [
                        'cmd_name' => 'network_speed_limit',
                        'cmd_params' => [
                            'type' => 'all',
                            'unit' => 'GB',
                            'value' => 999
                        ]
                    ]
                ]
            ],
            'report_status' => [
                'cmds' => [
                    ['cmd_name' => 'upload_now']
                ]
            ],
            'device_enable' => [
                'cmds' => [
                    ['cmd_name' => 'device_enable']
                ]
            ],
            'device_disable' => [
                'cmds' => [
                    ['cmd_name' => 'device_disable']
                ]
            ]
        ];

        $command = $commands[$type] ?? null;

        // 如果是需要参数的指令，合并传入的参数
        if ($command && !empty($params)) {
            $command = self::mergeCommandParams($command, $params, $type);
        }

        return $command;
    }
    
    /**
     * 验证指令类型是否支持
     * 
     * @param string $type 指令类型
     * @return bool
     */
    public static function isValidType(string $type): bool
    {
        $supportedTypes = array_keys(self::getSupportedTypes());
        // 同时支持getCommandByType中定义的指令
        $commandTypes = ['reboot', 'power_off', 'factory_reset', 'hotspot_enable', 'hotspot_disable',
                        'hotspot_config', 'hotspot_modify', 'sim_switch_slot1', 'sim_switch_slot2',
                        'speed_limit_10m', 'speed_limit_50m', 'speed_limit_100m', 'speed_unlimited',
                        'report_status', 'device_enable', 'device_disable'];
        return in_array($type, array_merge($supportedTypes, $commandTypes));
    }
    
    /**
     * 获取指令类型的详细信息
     *
     * @param string $type 指令类型
     * @return array|null
     */
    public static function getTypeInfo(string $type): ?array
    {
        $types = self::getSupportedTypes();
        return $types[$type] ?? null;
    }

    /**
     * 合并指令参数
     *
     * @param array $command 基础指令
     * @param array $params 用户传入的参数
     * @param string $type 指令类型
     * @return array 合并后的指令
     */
    private static function mergeCommandParams(array $command, array $params, string $type): array
    {
        // 对于hotspot_modify指令，直接使用用户传入的参数
        if ($type === 'hotspot_modify' && isset($command['cmds'][0])) {
            $command['cmds'][0]['cmd_params'] = $params;
        }

        // 可以在这里添加其他需要参数的指令处理逻辑

        return $command;
    }
}
